package com.codi.jobservice.controller;

import com.codi.base.util.DateUtils;
import com.codi.bus.core.service.FundCombinationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.codi.base.domain.BaseResult;
import com.codi.jobservice.result.BaseJobResult;
import com.codi.jobservice.service.QuartzService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 执行任务
 *
 * @author shi.pengyan
 * @date 2016年11月15日 上午9:52:08
 */
@Controller
@RequestMapping("/quartz")
public class QuartzController {

    private static final Logger logger = LoggerFactory.getLogger(QuartzController.class);
    @Autowired
    private QuartzService quartzService;

//    @Autowired
//    private FundCombinationService fundCombinationService;

    /**
     * 暂时手工调用 TODO
     *
     * @param jobName
     * @param groupName
     * @throws Exception
     */
    @RequestMapping(value = "/execute", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult executeJob(@RequestParam(value = "jobName") String jobName,
            @RequestParam(value = "groupName") String groupName) throws Exception {

        logger.info("jobName={},groupName={}", jobName, groupName);

        quartzService.triggerJob(jobName, groupName);

        BaseJobResult result = new BaseJobResult();
        result.setSuccess(true);
        result.setDescription("Please check Redis KEY, after 5s.");

        return result;
    }

    /**
     * 基金组合上线特殊需求
     */
//    @GetMapping("/pe")
//    @ResponseBody
//    public BaseResult pe() throws Exception {
//        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        Date beginDate = format.parse("2017-9-16");
//        Date endDate = format.parse("2018-1-18");
//        Calendar calendar = Calendar.getInstance();
//        logger.info("##################");
//        while (beginDate.before(endDate)) {
//            logger.info("##################1");
//            calendar.setTime(beginDate);
//            int dayweek = calendar.get(Calendar.DAY_OF_WEEK);
//
//            if (dayweek == 6 || dayweek == 7) {
//                beginDate = DateUtils.addDay(beginDate, 1);
//                continue;
//            }
//            logger.info("##################2");
//            fundCombinationService.updateFundCombination2(beginDate);
//            logger.info("##################3");
//            logger.debug("{}", beginDate);
//            beginDate = DateUtils.addDay(beginDate, 1);
//            logger.info("##################4");
//        }
//
//        return BaseResult.success();
//    }
}
